package com.lnb.common.utils;

public class BankCheckUtil {
    // 判断输入的字符串是否全部为数字
    public static boolean isNumeric(String strNum) {
        if (strNum == null) {
            return false;
        }
        return strNum.matches("\\d+");
    }

    // Luhn算法验证
    public static boolean validateCardNumber(String cardNumber) {
        if (!isNumeric(cardNumber)) {
            return false;
        }

        int sum = 0;
        boolean alternate = false;

        for (int i = cardNumber.length() - 1; i >= 0; i--) {
            int digit = Integer.parseInt(cardNumber.substring(i, i + 1));

            if (alternate) {
                digit *= 2;
                if (digit > 9) {
                    digit -= 9;
                }
            }

            sum += digit;
            alternate = !alternate;
        }

        return (sum % 10 == 0);
    }
}
